import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import electron from "vite-plugin-electron/simple";
import { fileURLToPath, URL } from "node:url";

// https://vitejs.dev/config/
export default defineConfig(({ command }) => {
  const isBuild = command === "build";
  return {
    plugins: [
      vue({
        template: {
          // 添加以下内容
          compilerOptions: {
            isCustomElement: (tag) => tag.startsWith("a-menu-group"),
          },
        },
      }),
      electron({
        externals: ["serialport"],
        main: {
          // Shortcut of `build.lib.entry`
          entry: "renderer.js",
          onstart({ startup }) {
            if (process.env.VSCODE_DEBUG) {
              console.log(
                /* For `.vscode/.debug.script.mjs` */ "[startup] Electron App"
              );
            } else {
              startup();
            }
          },
          vite: {
            minify: isBuild,
            build: {
              outDir: "dist",
            },
          },
        },
        preload: {
          // Shortcut of `build.rollupOptions.input`.
          // Preload scripts may contain Web assets, so use the `build.rollupOptions.input` instead `build.lib.entry`.
          input: "src/preload.js",
          vite: {
            minify: isBuild,
            build: {
              outDir: "dist",
            },
          },
        },
      }),
    ],
    resolve: {
      alias: {
        "@": fileURLToPath(new URL("./src", import.meta.url)),
      },
    },
    /*开发服务器选项*/
    server: {
      // 端口
      port: 9000,
      proxy: {
        "/api": {
          target: "https://douyin.xg360.cc/",
          ws: false,
          changeOrigin: true,
          pathRewrite: {
            "^/api": "", //需要rewrite的
          },
        },
      },
    },
  };
});
